<?php
namespace app\controller;

use app\BaseController;
use app\model\ArticleModel;
use app\model\ClassifyModel;
use think\facade\View;

/**
 * 留学资讯
 */
class News extends BaseController
{
    public function index($cid= 20)
    {

        $catid = $cid;
        View::assign('cid', $catid);

        $classify = ClassifyModel::where(['deleted' => 0, 'id'=>$catid])->find();
        View::assign('classify', $classify);
        // 获取资讯列表
        $this->getArticleList($catid);

        return View::fetch();
    }


    /**
     * 资讯详情
     */
    public function detail($id = 1)
    {


        // 获取资讯详情
        $article = ArticleModel::find($id);
        View::assign('data', $article);

        // 获取分类id
        $catid = $article['catid'];
        // 获取分类信息
        $clasify = ClassifyModel::find($catid);
        View::assign('clasify', $clasify);

        // 流量量 hits 自增1
        ArticleModel::where('id', $id)->inc('hits')->update();

        // 上一篇
        $prev = ArticleModel::where('id', '<', $id)->order('show_time', 'desc')->find();
        View::assign('prev', $prev);

        // 下一篇
        $next = ArticleModel::where('id', '>', $id)->order('show_time', 'asc')->find();
        View::assign('next', $next);



        return View::fetch();
    }


    /**
     * search
     */
    public function search()
    {
        View::assign('cid', 1);
        $keyword = input('keyword', '');
        $where = [
            'deleted' => 0,
            'status_flag' => 0,
        ];
        $list = ArticleModel::where($where)->where(
            'title', 'like', '%' . $keyword . '%'
        )->order( "show_time desc")->paginate(10);

        // 把分页数据赋值给模板变量list
        View::assign('list', $list);

        // 获取分页显示
        $page = $list->render();
        // 分页数据
        View::assign('page', $page);
        return View::fetch();
    }

}

